package com.first.orange.controller;

import com.first.orange.beans.Cart;
import com.first.orange.beans.UserLogin;
import com.first.orange.service.CartService;

import com.first.orange.service.PersonService;
import javafx.scene.control.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created on 2021/7/6.
 * <p>
 * Author: 190120
 * <p>
 * Description:
 */
@Controller

public class CartController {

    @Autowired
    private CartService cartService;
    private Integer userId;

    /*@RequestMapping("/add/{productId}")
    public String addCartItem(@PathVariable Integer productId, @RequestParam("quantity") Integer quantity,
                              HttpServletRequest request, HttpServletResponse response) {

        JDResult result = cartService.addCart(productId, quantity, request, response);
        return "cartSuccess";
    }*/

    /**
     *展示购物车表
     */
    @RequestMapping(value = "/cartList")
    public String cartsList(Map<String,Object> map, HttpSession session)throws  Exception{
        if (null==session.getAttribute("user") ||"".equals(session.getAttribute("user"))){
            return "redirect:/doLogin";
        }
        UserLogin userLogin=(UserLogin) session.getAttribute("user");
        userId=userLogin.getUserId();
        map.put("cartList",cartService.queryCartList(userId));
        return "home/shopcart";
    }


    /**
     *删除单行购物车信息
     */
    @RequestMapping(value = "/delCart/{carId}",method = RequestMethod.DELETE)
    public String delCarts(@PathVariable("carId") Integer carId){

        cartService.delCart(carId,userId);

        return "redirect:/cartList";
    }

    /**
     *勾选删除
     */
    @RequestMapping("/delByCarId")
    public String deleteAllCart(@RequestParam Integer carId){
        cartService.delCart(carId,userId);
        return "redirect:/cartList";
    }

    /**
     *加入收藏
     */
    @ResponseBody
    @RequestMapping("/addcoll")
    public boolean addColl(@RequestParam("productId") Integer productId,@RequestParam("carId") Integer carId){
        //cartService.delCart(carId,userId);
        if (cartService.selproductId(productId,userId) == null){
            cartService.addColle(productId,userId);

           return true;
        }


        return false;

    }


    /**
     *勾选加入收藏
     */

    @RequestMapping("/addByProductId")
    public String addByProductId(@RequestParam Integer productId){
        cartService.addColle(productId,userId);
        return "redirect:/cartList";
    }

    /**
     *加入购物车
     */
    @RequestMapping(value = "/insertCart")
    public String insertcart(Cart cart,HttpSession session){
        UserLogin userLogin=(UserLogin) session.getAttribute("user");
        cart.setUserId(userLogin.getUserId());
        cart.setChecked(1);
        if (null!=cartService.selectCartByUTPP(cart)){
            cartService.updataCart(cart);
        }else {
            cartService.addCarts(cart);
        }
        return "redirect:/cartList";
    }

    /**
     * @author : 武
     * @date : 2021/7/9
     * @return : java.lang.String
     * @description : 获取数据跳入订单页面，选中表示设为2，代表是点击立即购买获取的商品
     */
    @RequestMapping(value = "/insert")
    public String insertcart(Cart cart,HttpSession session,@RequestParam(defaultValue = "1") Integer checked){
        UserLogin userLogin=(UserLogin) session.getAttribute("user");
        cart.setUserId(userLogin.getUserId());
        cart.setChecked(checked);;
        cartService.addCarts(cart);
        if(checked == 1) {
            return "redirect:/cartList";
        }
        return "redirect:/showShopAddress?checked=" + checked;
    }

    /**
     *更新
     *
     */
    @ResponseBody
    @RequestMapping("/upcart")
    public boolean updataCart(@RequestParam("carId")String carId,@RequestParam("checked")String checked,@RequestParam("quantity")String quantity,@RequestParam("amount")String amount){

        return cartService.updataCarts(carId,checked,quantity,amount);
    }


    @ResponseBody
    @RequestMapping("/upallcart")
    public boolean updataAllCart(@RequestParam("checked")String checked,@RequestParam("che")String che){
        System.out.println("1111111111111111");
        return cartService.updataAllCarts(checked,che);
    }


}
